﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using SelfService.Model;
using WalkingTec.Mvvm.Core;
using SelfService.Model.Models;
using SelfService.ViewModel.SysDeviceVMs;
using TT;


namespace SelfService.ViewModel.SysClientVMs
{
    public partial class SysClientVm : BaseCRUDVM<SysClient>
    {
        [Display(Name = "功能列表")]
        public IEnumerable<string> SelectedModulesIDs { get; set; }
        [Display(Name = "硬件列表")]
        public IEnumerable<string> SelectedDevicesIDs { get; set; }
        [Display(Name = "卡片类型")]
        public IEnumerable<string> SelectedCardTypesIds { get; set; }
        [Display(Name = "支付类型")]
        public IEnumerable<string> SelectedPayTypesIds { get; set; }
        [Display(Name = "诊室列表")]
        public IEnumerable<string> SelectedRoomsIds { get; set; }

        [Display(Name = "医保操作员ID")]
        public string NationOperationId { get; set; }
        
        [Display(Name = "医保操作员名称")]
        public string NationOperationName { get; set; }
        
        public string Token { get; set; }

        public SysClientVm()
        {
            SetInclude(x => x.Modules);
            SetInclude(x => x.Devices);
            SetInclude(x => x.PayTypes);
            SetInclude(x => x.CardTypes);
            SetInclude(x =>x.Rooms);
            SetInclude(x => x.Theme);
            SetInclude(x => x.User);
        }

        public List<SysDeviceView> Devices { get; set; }

        public List<SysModule> Modules { get; set; }

        public List<SysOperator> Operators { get; set; }

        public List<SysCardType> CardTypes { get; set; }

        public List<SysPayType> PayTypes { get; set; }

        public List<SysDoctor> Doctors { get; set; }

        public List<SysRoom> Rooms { get; set; }

        [Display(Name = "主题")]
        public string ThemeName { get; set; }

        protected override void InitVM()
        {
            NationOperationName = Entity.User.NationOperationName;
            NationOperationId = Entity.User.NationOperationId;
            SelectedModulesIDs = Entity.Modules?.Select(x => x.ModuleId.ToString());
            SelectedDevicesIDs = Entity.Devices?.Select(x => x.DeviceId.ToString());
            SelectedCardTypesIds = Entity.CardTypes?.Select(e => e.CardTypeId.ToString());
            SelectedPayTypesIds = Entity.PayTypes?.Select(e => e.PayTypeId.ToString());
            SelectedRoomsIds = Entity.Rooms?.Select(e => e.RoomId.ToString());
            this.Devices = DC.Set<ClientDevice>()
                .Where(e => e.ClientId == Entity.ID)
                .Include(e => e.Device.Group)
                .Include(e => e.Device)
                .Select(e => new SysDeviceView()
                {
                    ID = e.ID,
                    Name = e.Device.Name,
                    Model = e.Device.Model,
                    Description = e.Device.Description,
                    Mode = e.Device.Mode,
                    GroupName = e.Device.Group.Name,
                    Port = e.Device.Port,
                    DllName = e.Device.DllName,
                    DllZipId = e.Device.DllZipId,
                    DllClass = e.Device.DllClass,
                    DllWorkSpace = e.Device.DllWorkSpace,
                    DllHash = e.Device.DllHash,
                    DllZipName = e.Device.DllZip.FileName,
                    IsEnable = e.Device.IsEnable,
                    IsSwallow = e.Device.IsSwallow
                })
                .ToList();
            this.Modules = DC.Set<ClientModules>()
                .Where(e => e.ClientId == Entity.ID)
                .Include(e => e.Module)
                .Select(e => e.Module)
                .ToList();
            this.Operators = DC.Set<SysOperator>()
                .Where(e => e.ClientId == Entity.ID && e.IsEnable == CommonYesOrNo.是)
                .ToList();
            this.CardTypes = DC.Set<ClientCardType>()
                .Include(e => e.CardType)
                .Where(e => e.ClientId == Entity.ID)
                .Select(e => e.CardType)
                .ToList();
            this.PayTypes = DC.Set<ClientPayTypes>()
                .Include(e => e.PayType)
                .Where(e => e.ClientId == Entity.ID)
                .Select(e => e.PayType)
                .ToList();
            List<Guid> roomIds = Entity.Rooms?.Select(e => e.RoomId).ToList();
            this.Rooms = DC.Set<SysRoom>()
                .Where(e => roomIds.Contains(e.ID))
                .OrderBy(e => e.Name)
                .ToList();
            this.Doctors = DC.Set<SysDoctor>()
                .Where(e => e.Workday >= DateTime.Today && e.Workday < DateTime.Today.AddDays(1) && roomIds.Contains((Guid)e.RoomId))
                .Include(e => e.Room)
                .Include(e => e.WorkTimeIntervals)
                .OrderBy(e => e.Room.Name)
                .ToList();
            foreach (var doctor in Doctors)
            {
                var ids = doctor.WorkTimeIntervals.Select(e => e.ID).ToList();
                doctor.WorkTimeIntervals = DC.Set<DoctorWorkTimeInterval>()
                    .Where(e => ids.Contains(e.ID))
                    .Include(e => e.TimeInterval)
                    .ToList();
            }
        }

        public override void DoAdd()
        {
            var user = DC.Set<FrameworkUser>()
                .FirstOrDefault(e => e.ID == Entity.UserId);
            if (user == null)
            {
                MSD.AddModelError("UserId", "请选择用户");
                return;
            }
            if (!user.IsValid)
            {
                MSD.AddModelError("UserId", "用户被禁用");
                return;
            }
            Entity.Name = user.Name;
            Entity.Code = user.ITCode;
            Entity.Address = user.Address;

            Entity.Modules = new List<ClientModules>();
            if (SelectedModulesIDs != null)
            {
                foreach (var id in SelectedModulesIDs)
                {
                     ClientModules middle = new ClientModules();
                    middle.SetPropertyValue("ModuleId", id);
                    Entity.Modules.Add(middle);
                }
            }

            Entity.Devices = new List<ClientDevice>();
            if (SelectedDevicesIDs != null)
            {
                foreach (var id in SelectedDevicesIDs)
                {
                     ClientDevice middle = new ClientDevice();
                    middle.SetPropertyValue("DeviceId", id);
                    Entity.Devices.Add(middle);
                }
            }

            Entity.CardTypes = new List<ClientCardType>();
            if (SelectedCardTypesIds != null)
            {
                foreach (var id in SelectedCardTypesIds)
                {
                    ClientCardType middle = new ClientCardType();
                    middle.SetPropertyValue("CardTypeId", id);
                    Entity.CardTypes.Add(middle);
                }
            }


            Entity.PayTypes = new List<ClientPayTypes>();
            if (SelectedPayTypesIds != null)
            {
                foreach (var id in SelectedPayTypesIds)
                {
                    ClientPayTypes middle = new ClientPayTypes();
                    middle.SetPropertyValue("PayTypeId", id);
                    Entity.PayTypes.Add(middle);
                }
            }

            Entity.Rooms = new List<ClientRooms>();
            if (SelectedRoomsIds != null)
            {
                foreach (var id in SelectedRoomsIds)
                {
                    ClientRooms middle = new ClientRooms();
                    middle.RoomId = Guid.Parse(id);
                    Entity.Rooms.Add(middle);
                }
            }



            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            var user = DC.Set<FrameworkUser>()
                .FirstOrDefault(e => e.ID == Entity.UserId);
            if (user == null)
            {
                MSD.AddModelError("UserId", "请选择用户");
                return;
            }
            if (!user.IsValid)
            {
                MSD.AddModelError("UserId", "用户被禁用");
                return;
            }
            Entity.Name = user.Name;
            Entity.Code = user.ITCode;
            Entity.Address = user.Address;
            Entity.Modules = new List<ClientModules>();
            if(SelectedModulesIDs != null )
            {
                foreach (var item in SelectedModulesIDs)
                {
                    ClientModules middle = new ClientModules();
                    middle.SetPropertyValue("ModuleId", item);
                    Entity.Modules.Add(middle);
                }
            }

            Entity.Devices = new List<ClientDevice>();
            if(SelectedDevicesIDs != null )
            {
                foreach (var item in SelectedDevicesIDs)
                {
                    ClientDevice middle = new ClientDevice();
                    middle.SetPropertyValue("DeviceId", item);
                    Entity.Devices.Add(middle);
                }
            }

            Entity.CardTypes = new List<ClientCardType>();
            if (SelectedCardTypesIds != null)
            {
                foreach (var id in SelectedCardTypesIds)
                {
                    ClientCardType middle = new ClientCardType();
                    middle.SetPropertyValue("CardTypeId", id);
                    Entity.CardTypes.Add(middle);
                }
            }

            Entity.PayTypes = new List<ClientPayTypes>();
            if (SelectedPayTypesIds != null)
            {
                foreach (var id in SelectedPayTypesIds)
                {
                    ClientPayTypes middle = new ClientPayTypes();
                    middle.SetPropertyValue("PayTypeId", id);
                    Entity.PayTypes.Add(middle);
                }
            }

            Entity.Rooms = new List<ClientRooms>();
            if (SelectedRoomsIds != null)
            {
                foreach (var id in SelectedRoomsIds)
                {
                    ClientRooms middle = new ClientRooms();
                    middle.RoomId = Guid.Parse(id);
                    Entity.Rooms.Add(middle);
                }
            }

            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
